﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using admin.ServiceReference1;
using System.Windows.Browser;

namespace admin.two
{
    public partial class set : UserControl
    {
        ServiceReference1.Service1Client sc;

        public set()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(set_Loaded);
            this.bt_cancel.Click += new RoutedEventHandler(bt_cancel_Click);
            this.bt_post.Click += new RoutedEventHandler(bt_post_Click);
        }

        void sc_setdataCompleted(object sender, setdataCompletedEventArgs e)
        {
            bool sum = e.Result;
            if (sum)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("操作失败");
            }
        }

        void bt_post_Click(object sender, RoutedEventArgs e)
        {
            sc.setdataAsync(tbk_key.Text, tb_value.Text);
            bt_cancel_Click(this, new RoutedEventArgs());
        }

        bool first = true;

        void bt_cancel_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "endedit", true);
        }

        void gridview_SelectionChanged(object sender, EventArgs e)
        {
            if (!first)
            {
                setting selectdata = (setting)gridview.SelectedItem;
                if (selectdata != null)
                {
                    tbk_id.Text = selectdata.id.ToString();
                    tbk_key.Text = selectdata.set_name.ToString();
                    tb_value.Text = selectdata.set_value.ToString();
                    VisualStateManager.GoToState(this, "beginedit", true);
                }
            }
            first = false;
        }

        void set_Loaded(object sender, RoutedEventArgs e)
        {
            sc = serverManager.getPox();
            sc.getsettingCompleted += new EventHandler<admin.ServiceReference1.getsettingCompletedEventArgs>(sc_getsettingCompleted);
            sc.getsettingAsync();
            gridview.SelectionChanged +=new SelectionChangedEventHandler(gridview_SelectionChanged);
            sc.setdataCompleted += new EventHandler<setdataCompletedEventArgs>(sc_setdataCompleted);
        }

        void sc_getsettingCompleted(object sender, admin.ServiceReference1.getsettingCompletedEventArgs e)
        {
            List<setting> data = new List<setting>(e.Result);
            gridview.ItemsSource = data;
        }
    }
}
